package edu.ustb.aur.controller;

import com.alibaba.fastjson.JSONObject;
import edu.ustb.aur.chain.Chain;
import edu.ustb.aur.chain.ChainService;
import edu.ustb.aur.chain.interactive.ChainInteractiveService;
import edu.ustb.aur.myenum.ResultCodeEnum;
import edu.ustb.aur.result.Result;
import edu.ustb.shellchainapi.shellchain.command.ShellChainException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @author ShiZhe
 * @create 2021-11-30 22:55
 * 链的前后端交互
 */
@Api(tags = "链控制器")
@RestController
@RequestMapping("chain")
@CrossOrigin
public class ChainController {

    /**
     * 链类存储服务
     */
    @Autowired
    private ChainService chainService;

    /**
     * 资产链
     */
    @Autowired
    private ChainInteractiveService chainInteractiveServiceAsset = new ChainInteractiveService("OBJ");

    /**
     * 根据合约id获取交易id集合
     *
     * @param contractId 合约id
     * @return 合约涉及交易id集合
     */
    @ApiOperation(value = "findContractTxIdListById", notes = "根据合约id返回交易id集合")
    @GetMapping("findContractTxIdListById/{contractId}")
    public Result findContractTxIdListById(@PathVariable String contractId) {

        boolean contractIdIsExist = chainService.findContractIdIsExist(contractId);
        if (contractIdIsExist) {
            List<String> contractTxIdListById = chainService.findContractTxIdListById(contractId);
            return Result.ok(contractTxIdListById);
        } else {
            return Result.fail(Result.build(false, ResultCodeEnum.CONTRACT_ID_EXIST));
        }
    }

    /**
     * 根据交易id从区块链获取交易信息
     *
     * @param txId 交易id
     * @return 交易信息
     * @throws ShellChainException 异常抛出
     */
    @ApiOperation(value = "getMessageByTxId", notes = "根据txId返回交易信息")
    @GetMapping("getMessageByTxId/{txId}")
    public Result getMessageByTxId(@PathVariable String txId) throws ShellChainException {
        String fromChain = chainInteractiveServiceAsset.getFromChain(txId);
        JSONObject jsonObject = JSONObject.parseObject(fromChain);
        System.out.println(jsonObject);
        if (jsonObject == null) {
            return Result.fail(Result.build(null, ResultCodeEnum.DATA_ERROR));
        }
        return Result.ok(jsonObject);
    }

    /**
     * 查询所有链的所有信息
     * @return
     */
    @ApiOperation(value = "findAllChain", notes = "查询所有链的所有信息")
    @GetMapping("findAllChain")
    public Result findAllChain() {
        // 调用service方法
        List<Chain> chainList = chainService.list();
        return Result.ok(chainList);
    }

    /**
     * 文件上传
     * @param file 文件
     * @return 返回交易地址
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws ShellChainException, IOException {
        // 文件序列化
        byte[] bytes = file.getBytes();
        //上链参数
        HashMap<String, Object> attributes = new HashMap<>();
        attributes.put("file",bytes);
        // 获取链交易id
        String txId = chainInteractiveServiceAsset.sendToChain(
                "18XEZTrTtqQVfDxVafcJoBd37xrtcBvH4Cs4yV", 0, attributes);
        //返回
        return Result.ok(txId);
    }
}
